<!--Copyright 2024 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

# Adding support for an unsupported architecture

We welcome contributions to extend the functionality of ExecuTorch export. This guide provides high-level instructions for contributors who want to:

1. Export a new model that is not currently supported.
2. Add new recipes or support a new task for export.

---

## Exporting a New Model

If you want to export a model that is not already supported by the library, follow these steps:

### Step 1: Export and Test the Model
1. Attempt to export and lower the model using an existing task and recipe. On success, it will store the exported model in a `.pte` file.
2. Add a test case for the model in the appropriate test suite.
   - For example, you can make sure tests pass for the new `my_new_model` by running:
     ```bash
     pytest tests/executorch/export/test_*.py -k "test_my_new_model"  # doctest: +SKIP
     pytest tests/executorch/runtime/test_*.py -k "test_my_new_model"  # doctest: +SKIP
     ```

### Step 2: Handle Export Failures
1. If the export fails in Step 1, report the issue by opening a GitHub issue.
2. If the issue requires changes to the model’s architecture or its Hugging Face implementation, these modifications may be made upstream in the Hugging Face Transformers library.

---

## Adding New Recipes or Tasks

To extend ExecuTorch with new recipes or tasks, follow these guidelines:

### Registering a New Recipe
You can add a custom recipe to define specific optimizations or configurations for exporting models. Below is an example:

```python
from exporters.executorch import register_recipe

@register_recipe("my_custom_recipe")
def export_with_custom_recipe(model, config, *args, **kwargs):
    # Example: Apply a custom quantization
```

### Registering a Task
The task registration process is same as adding a recipe. Besides that you may need to implement a new `ExecuTorchModelForXXX` class.
